module.exports = app => {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema;
    const TemplateSchema = new Schema({
        imgUrl: String, // 图片地址
        target: { type: Schema.Types.ObjectId, ref: 'Target' }, // 目标链接地址
        shareCounts: { // 分享数
            type: Number,
            default: 0
        },
        visitCounts: { // 访问数
            type: Number,
            default: 0
        },
        status: { // 状态 0 未发布 1 已发布 2 已过期
            type: Number,
            default: 0
        },
        author: { type: Schema.Types.ObjectId, ref: 'User' }, // 作者
        description: String, // 描述
        type: Number, // 类型
    }, { timestamps: { createdAt: 'createDate', updatedAt: 'updateDate' } });
    TemplateSchema.pre('findOne', function () {
        this.populate('author', '_id name sex avatarUrl role');
        this.populate('target', '_id url shareType');
    });
    TemplateSchema.pre('find', function () {
        this.populate('author', '_id name sex avatarUrl -role');
        this.populate('target', '_id url shareType');
    });
    TemplateSchema.pre('findOneAndUpdate', function () {
        this.populate('author', '_id name sex avatarUrl role');
        this.populate('target', '_id url shareType');
    });
    return mongoose.model('Template', TemplateSchema);
}